<template>
  <div>
    <el-upload
      :action="uploadUrl"
      list-type="picture-card"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      :on-success="handleSuccess"
      :file-list="fileList"
      ref="upload"
      multiple
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>

<script>
import { uploadUrl, host } from "@/api/base";
export default {
  props: {
    fileList:{
        type:Array,
        default:function(){
            return []
        }
    }
  },
  data() {
    return {
      uploadUrl,
      //fileList: [], //[{name:'xxx.jpg',url:'http://aaa.bbb.com/xxx.jpg'}]
      dialogImageUrl: "",
      dialogVisible: false,
    };
  },
  methods: {
    handleSuccess(response, file, fileList) {
      console.log("成功", response, file, fileList);
      //获取上传后服务器返回的地址，并处理成http://localhost:7788/1679571492514-1.jpg
      let imgUrl = host + "/" + response.url.slice(7);
      this.$emit("sendImg", imgUrl);
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    clear() {
      this.$refs.upload.clearFiles();
    },
  },
};
</script>

<style>
</style>